package mobile.pubcrawler.ui;

import mobile.pubcrawler.FooterView;
import mobile.pubcrawler.PubCrawlerUI;
import mobile.pubcrawler.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainUI extends Activity {

	FooterView footer;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
    	setContentView(R.layout.main);
    	this.setTitle(getString(R.string.app_name));
    	
    	ImageButton pubTourButton = (ImageButton)this.findViewById(R.id.mainPubTourButton);
    	pubTourButton.setOnClickListener(mainPubTourButtonListener);
    	ImageButton nextPubButton = (ImageButton)this.findViewById(R.id.mainNextPubButton);
    	nextPubButton.setOnClickListener(mainNextPubButtonListener);
    	ImageButton settingsButton = (ImageButton)this.findViewById(R.id.mainSettingsButton);
    	settingsButton.setOnClickListener(mainSettingsButtonListener);
    	ImageButton currCrawlButton = (ImageButton)this.findViewById(R.id.mainCurrCrawlButton);
    	currCrawlButton.setOnClickListener(mainCurrCrawlButtonListener);
    	
    	footer = (FooterView)this.findViewById(R.id.mainFooterView);
    	footer.setGravity(Gravity.CENTER_HORIZONTAL);
    	footer.setOnClickListener(footerClickListener);
	}
	
	public void onResume() {
		super.onResume();
		if (footer!=null) footer.update();
	}
	
	 // ------------------- Button listeners --------------------  
    private OnClickListener mainPubTourButtonListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startSearchPubUI(); } };
    private OnClickListener mainNextPubButtonListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startNextPubUI(); } };
    private OnClickListener mainSettingsButtonListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startSettingsUI(); } };
    private OnClickListener mainCurrCrawlButtonListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startCurrentTourUI(); } };
    private OnClickListener footerClickListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startStartLocationUI(); } };
    
    //----------------- Menu inflaters and listeners -------------------
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }
    
    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuMainMenu:
        	PubCrawlerUI.getInstance().startMainUI();
            return true;
        case R.id.menuSettings:
        	PubCrawlerUI.getInstance().startSettingsUI();
            return true;
        case R.id.menuQuit:
            this.finish();
            PubCrawlerUI.getInstance().finish();
            return true;
        }
        return false;
    }
    
}
